package com.hr.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hr.entity.BaseInfo;
import com.hr.entity.Job;
import com.hr.entity.Register;
import com.hr.entity.vo.RegisterQuery;
import com.hr.entity.vo.RegisterVo;
import com.hr.entity.vo.RegisterExlVo;
import com.hr.service.BaseInfoService;
import com.hr.service.JobService;
import com.hr.service.RegisterService;
import com.hr.service.UserService;
import com.hr.utils.ConstantUtil;
import com.hr.utils.R;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hr
 * @since 2021-10-15
 */
@CrossOrigin
@RestController
@RequestMapping("/hr/service/register")
public class RegisterController {

    @Autowired
    RegisterService registerService;

    @Autowired
    UserService userService;

    @Autowired
    JobService jobService;

    @Autowired
    BaseInfoService baseInfoService;

    @PostMapping("registers/{cur}/{limit}")
    public R get_registers_page(@PathVariable("cur") long cur,
                           @PathVariable("limit") long limit,
                           @RequestBody RegisterQuery registerQuery) {

        Page<Register> page = new Page<>(cur, limit);
        QueryWrapper<Register> wrapper = new QueryWrapper<>();

        Integer status = registerQuery.getStatus();
        Integer result = registerQuery.getResult();
        String jobName = jobService.getById(registerQuery.getJobId()).getJobName();

        wrapper.eq("job_id", registerQuery.getJobId());
        if(status != null) {
            wrapper.eq("status", status);
        }
        if(result != null) {
            wrapper.eq("result", result);
        }

        registerService.page(page, wrapper);

        long total = page.getTotal();
        List<Register> records = page.getRecords();
        List<RegisterVo> registerVos = new ArrayList<>();

        for(Register register: records) {
            RegisterVo registerVo = new RegisterVo();
            BeanUtils.copyProperties(register, registerVo);
            registerVo.setUserName(userService.getById(registerVo.getUserId()).getUsername());
            registerVo.setJobName(jobName);
            registerVos.add(registerVo);
        }
        return R.ok().data("total", total).data("registers", registerVos);
    }

    @GetMapping("register")
    public R get_register(String id) {
        Register register = registerService.getById(id);
        return R.ok().data("register", register);
    }

    @PostMapping("register")
    public R add_register(@RequestBody Register register) {
        boolean save = registerService.save(register);
        return save ? R.ok() : R.error();
    }

    @PutMapping("register")
    public R update_register(@RequestBody Register register) {
        boolean update = registerService.updateById(register);
        return update ? R.ok() : R.error();
    }

    @DeleteMapping("register/{id}")
    public R delete_register(@PathVariable("id") String id) {
        boolean delete = registerService.removeById(id);
        return delete ? R.ok() : R.error();
    }

    @PutMapping("publish/{id}/{pass}")
    public R publish_register(@PathVariable("id") Integer id, @PathVariable("pass") Integer pass) {
        Register register = registerService.getById(id);
        if(pass == 0) {
            register.setResult(2);
        } else {
            register.setStatus(register.getStatus() + 1);
            if(register.getStatus() == 3) {
                register.setResult(1);
            } else {
                register.setResult(0);
            }
        }

        boolean update = registerService.updateById(register);
        return update ? R.ok() : R.error();
    }

    @GetMapping("print/{jobId}")
    public R print_registers(@PathVariable("jobId") String jobId) throws IOException {
        QueryWrapper<Register> wrapper = new QueryWrapper<>();

        String jobName = jobService.getById(jobId).getJobName();

        wrapper.eq("job_id", jobId);

        List<Register> registers = registerService.list(wrapper);
        List<RegisterExlVo> registerExlVos = new ArrayList<>();

        for(Register register: registers) {
            RegisterExlVo registerExlVo = new RegisterExlVo();
            BeanUtils.copyProperties(register, registerExlVo);
            registerExlVo.setUserName(userService.getById(registerExlVo.getUserId()).getUsername());
            registerExlVo.setJobName(jobName);
            registerExlVos.add(registerExlVo);
            switch (register.getStatus()) {
                case 0:
                    registerExlVo.setStatus("简历测评");
                    break;
                case 1:
                    registerExlVo.setStatus("笔试测评");
                    break;
                case 2:
                    registerExlVo.setStatus("面试测评");
                    break;
                case 3:
                    registerExlVo.setStatus("录用");
                    break;
            }
            switch (register.getResult()) {
                case 0:
                    registerExlVo.setResult("正在进行");
                    break;
                case 1:
                    registerExlVo.setResult("通过");
                    break;
                case 2:
                    registerExlVo.setResult("淘汰");
                    break;
            }
        }

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(jobName + "招聘详情",jobName + "招聘详情"),
                RegisterExlVo.class, registerExlVos);

        workbook.write(new FileOutputStream("/Users/aaa/Documents/detail/" + jobName + "招聘详情.xlsx"));

        return R.ok();
    }

    @PostMapping("sign_up/{jobId}")
    public R sign_up(@PathVariable("jobId") Integer jobId) {
        QueryWrapper<BaseInfo> baseInfoQueryWrapper = new QueryWrapper<>();
        baseInfoQueryWrapper.eq("user_id", ConstantUtil.CURRENT_USER);
        BaseInfo one = baseInfoService.getOne(baseInfoQueryWrapper);
        if(one == null) {
            return R.error().message("请先前往个人中心完善简历");
        }
        QueryWrapper<Register> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", ConstantUtil.CURRENT_USER);
        wrapper.eq("job_id", jobId);
        Register register = registerService.getOne(wrapper);
        if(register != null) {
            return R.error().message("您已经报过名了");
        }
        Job job = jobService.getById(jobId);
        job.setSurplus(job.getSurplus() - 1);
        jobService.updateById(job);
        register = new Register();
        register.setUserId(ConstantUtil.CURRENT_USER);
        register.setJobId(jobId);
        register.setStatus(0);
        register.setResult(0);
        registerService.save(register);
        return R.ok();
    }

    @GetMapping("registerByUserId")
    public R getRegisterByUserId() {
        QueryWrapper<Register> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", ConstantUtil.CURRENT_USER);
        List<Register> registers = registerService.list(wrapper);
        return R.ok().data("registers", registers);
    }


}

